package com.itheima.mapper;

import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.vto.OrderVto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;

@Mapper
public interface OrderMapper {
    @Select("select orderType,name,orderDate,setmeal_id from t_order to2  left join t_member tm on tm.id = to2.member_id where to2.id = #{id} ")
    OrderVto findById(Integer id);

    /**
     * 判断是否存在这个会员
     * @param idCard
     * @return
     */
    @Select("SELECT COALESCE((SELECT id FROM t_member WHERE phoneNumber = #{Telephone}), 0) as id")
    int QueryID(String Telephone);

    /**
     * 注册会员
     * @param member
     */
    @Insert("insert into t_member(name, sex, idCard, phoneNumber, regTime) values(#{name},#{sex},#{idCard},#{phoneNumber},#{regTime})")
    void register(Member member);

    /**
     * 查询该日期存在可预约人数
     * @param orderDate
     * @return
     */
    @Select("select number from t_ordersetting where orderDate = #{orderDate}")
    Integer ValidateOrderSetting(LocalDate orderDate);

    /**
     * 查找已经预约人数
     * @param orderDate
     * @return
     */
    @Select("select reservations  from t_ordersetting where orderDate = #{orderDate}")
    int FindOrdered(LocalDate orderDate);

    /**
     * 是否预约过
     * @param idCard
     * @param orderDate
     * @return
     */
    @Select("select count(*) from t_order left join t_member tm on tm.id = t_order.member_id where tm.idCard = #{idCard} and orderDate = #{orderDate}")
    int isExit(String idCard, LocalDate orderDate);

    /**
     * 增加预约
     * @param order
     */
    @Insert("insert into t_order( member_id, orderDate, orderType, orderStatus, setmeal_id) value (#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    void addOrder(Order order);
}

